/**
 * Created by yihua on 2017/2/19.
 */
$(document).ready(function () {
    initRegionInfo();
    initGoodsInfo();
    $("#confirm").unbind('click').click(function () {
        addRetionOrder();
    });


});


function addRetionOrder() {
    var options = {
        url: '/home/shop/addregiongoods',
        beforeSubmit: validate,  // pre-submit callback
        success: showResponse,  // post-submit callback
        dataType: 'json',
        type: 'post',
        data: {
            'goods_name': $("#goods_id").find("option:selected").text()
        }
    };
    //$("#regionGoodsForm").ajaxSubmit(options)
    $("#regionGoodsForm").ajaxSubmit(options)
}

function validate(formData, jqForm, options) {
    for (var i = 0; i < formData.length; i++) {
        //console.log(formData[i].name)
        if (formData[i].name == 'file') {
            break;
        }
        if (!formData[i].value) {
            toastr.error('请输入所有选项', '', {positionClass: 'toast-bottom-center'});
            return false;
        }
    }
}

function showResponse(responseText, statusText, xhr, $form) {
    console.log(responseText);
    if (0 !== responseText.errno) {
        toastr.error(responseText.errmsg, '', {positionClass: 'toast-bottom-center'});
    }
    else {
        toastr.success('增加成功', '', {positionClass: 'toast-bottom-center'});
        $('#myModal').modal('hide');
    }
}


function initRegionInfo() {
    $.ajax({
        url: '/home/shop/getregioninfo',
        timeout: 100000,
        type: 'get',
        dataType: 'json',
        data: {
            'sub': true,
        },
        success: function (rs) {
            console.log(rs)
            if (rs.errno != 0) {
                toastr.error(rs.errno);
                return false;
            } else {
                var data = [];
                for (var i = 0; i < rs.data.length; i++) {
                    data.push({'id': rs.data[i].rid, 'text': rs.data[i].name})
                }
                console.log(data)
                $("#rid").select2({
                    dropdownParent: $("#myModal"),
                    data: data
                })
            }
        },
        error: function () {
            toastr.error('系统错误');
        }
    });
}

function initGoodsInfo() {
    $.ajax({
        url: '/home/shop/goodslist',
        timeout: 100000,
        type: 'get',
        dataType: 'json',
        data: {
            'type': 'list',
        },
        success: function (rs) {
            console.log(rs)
            if (rs.errno != 0) {
                toastr.error(rs.errno);
                return false;
            } else {
                var myData = [];
                for (var i = 0; i < rs.data.length; i++) {
                    myData.push({'id': rs.data[i].goods_id, 'text': rs.data[i].goods_name})
                }
                //console.log(myData)
                $("#goods_id").select2({
                    dropdownParent: $("#myModal"),
                    data: myData
                })
            }
        },
        error: function () {
            toastr.error('系统错误');
        }
    });
}